const express = require("express")
const router = express.Router()
const resultClass = require("../dto/result")
const {query} = require("../mysql/index")

// 查询文章接口
router.get("/findEssay",async (req,res)=>{
    const body = req.query
    console.log("请求体----------",body);
    console.log("请求体----------",body.page);
    console.log("请求体----------",body.pagesize);
    const sql = `select * from sys_essay limit ${body.page*body.pagesize},${body.pagesize}`
    const selectResult = await query(sql)
    console.log("查询文章列表",selectResult);
    const result = new resultClass(0,selectResult)
    res.send(result)
})
// 添加文章接口
router.post("/addEssay",async (req,res)=>{
    const {title,des,user} = req.body
    console.log("请求体----------",title,des,user); //  12121 21212 admin
    let result = {}
    if(!title||!des){
        result = new resultClass('-1',['标题或者描述不能为空'])
    }else{
        const sql = `insert into sys_essay (title,des,user) values ('${title}','${des}','${user}')`
        const selectResult = await query(sql)
        console.log("查询文章列表",selectResult);
        result = new resultClass(0,selectResult)
    }
    res.send(result)
})

module.exports = router